require(['./config'],() =>{
    require(['template','header','footer','bootstrap'],(template) =>{
        class submitOrder {
            constructor () {
                this.rendonSubmitOrder().then(() =>{
                    this.cele()
                })
                this.addSite()
                this.rendonsite().then(() =>{
                    this.bindSiteEvent()
                    this.goPay()
                })
            }
            rendonSubmitOrder () {
                return new Promise ((reslove) =>{
                    this.cart = localStorage.getItem('cart')
                    this.cart = JSON.parse(this.cart)
                    if(this.cart){
                        this.cart = this.cart.filter((shop) => shop.check)
                        var listTpl = template('sugoodlisttemplate',{cart:this.cart})
                        $('#sugoodlist').html(listTpl)
                        reslove()
                    }
                })
            }
            cele () {
                var count = this.cart.reduce((num,shop) =>{
                    num += shop.count
                    return num
                },0)
                $('.total-goods').html(count)

                var total = this.cart.reduce((total,shop) =>{
                    total += shop.count * shop.price
                    return total
                },0)
                total = total.toFixed(2)
                $('.money').find('span').html(`￥${total}`)
                $('.total-price').html(`￥${total}`)
            }
            addSite () {
                $('.add-ok').on('click',() =>{
                    let name = $('.sh-name').val(),
                        tele = $('.sh-tele').val(),
                        site = $('.sh-site').val()
                    if(name && tele && site){
                        $.get('http://localhost/project/dist/api/list/address.php',{name,tele,site},(resp) =>{
                            $('.hint').show().html(resp.body.msg)
                            if(resp.code === 200){
                                setTimeout(() =>{
                                    $('#address-s').modal('hide')
                                },2000)
                            }
                            setTimeout(() =>{
                                $('.hint').hide().html('')
                                this.rendonsite()
                            },2000)
                        },'json')
                    }else{
                        alert('姓名、地址、电话不能为空')
                    }
                })
            }
            rendonsite () {
                return new Promise((reslove) =>{
                    $.get('http://localhost/project/dist/api/list/rendonSite.php',(resp) =>{
                        var {list} = resp.body
                        if(resp.code === 200){
                            let siteTpl = template('itemTemplate',{list})
                            $('#rendonitem').html(siteTpl)
                            $('.address-list').show()
                            reslove()
                        }
                    },'json')
                })
            }
            bindSiteEvent () {
                var _this = this
                $('#rendonitem').on('click',function (e){
                    var tr = $(e.target).parents('tr')
                    var id = tr.data('id')
                    if($(e.target).is('.btn-edit')){
                        tr.addClass('edit')
                        tr.find('.suspan').each((i,item) =>{
                           tr.find('.suinput').eq(i).val($(item).html())
                        })
                    }else if($(e.target).is('.btn-cancel')){
                        tr.removeClass('edit')
                    }else if($(e.target).is('.btn-ok')){
                        let name = tr.find('.suname').val(),
                            address = tr.find('.suaddress').val(),
                            tele = tr.find('.sutele').val()
                        $.get('http://localhost/project/dist/api/list/updeteSite.php',{name,address,tele,id},resp =>{
                            if(resp.code === 200){
                                tr.find('.suinput').each((i,item) =>{
                                    tr.find('.suspan').eq(i).html($(item).val())
                                })
                                tr.removeClass('edit')
                            }
                        },'json')
                    }else if($(e.target).is('.btn-del')){
                        $.get('http://localhost/project/dist/api/list/updeteSite.php',{},resp =>{
                            console.log(resp)
                            if(confirm('确定要删除吗?')){
                                if(resp.code === 200){
                                    alert('删除成功')
                                }
                            }
                        },'json')
                    }
                })
            }
            goPay () {
                $('.go-pay').on('click',() =>{
                    var n = 0
                    console.log($('.check'))
                    $('.check').each((i,item,) =>{
                        if($(item).prop('checked')){
                            n++
                        }
                    })
                    if(n == 1){
                        var myorder = localStorage.getItem('myorder')
                        if(myorder){
                            myorder = JSON.parse(myorder)
                            this.cart.forEach((item) =>{
                                myorder.push(item)
                            })
                            localStorage.setItem('myorder',JSON.stringify(myorder))
                        }else{
                            console.log(1)
                            localStorage.setItem('myorder',JSON.stringify(this.cart))
                        }
                        
                        var cart = localStorage.getItem('cart')
                        cart = JSON.parse(cart)
                        cart = cart.filter(item => item.check!=true)
                        if(cart.length==0){
                            localStorage.removeItem('cart')
                        }else{
                            localStorage.setItem('cart',[JSON.stringify(cart)])
                        }

                        alert('支付成功,即将跳转订单页面')
                        window.open('/html/myOrder.html','_self')
                    }else{
                        alert('地址不能为空且只能选取一个')
                    }
                })
            }
        }
        new submitOrder()
    })
})